package com.longrise.android.byjk.plugins.tabfirst;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2017/11/23.
 * 首页 获取批量购买的数据 等 (这些数据没有配置,需要单独获取)
 */

public class HomeGetDataHelper {

    /**
     * 首页 批量购买的数据 最多3个
     */
    public static void getBatchbuydata(final BatchbuyResult callback) {
        //第一次加载的时候先使用本地数据
        EntityBean[] bbt_course_allCoursePay = CacheUtils.getCacheFirstData("bbt_course_allCoursePay");
        if (callback != null) {
            callback.onSuccess(bbt_course_allCoursePay);
        }

        EntityBean bean = new EntityBean();
        bean.set("state", 0);  //0代表 返回三个数据
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "bbt_course_allCoursePay", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean resultBean = bean.getBean("result");
                                EntityBean[] allcourses = resultBean.getBeans("allcourse");
                                CacheUtils.cacheFirstData("bbt_course_allCoursePay", allcourses);
                                if (callback != null) {
                                    callback.onSuccess(allcourses);
                                }
                            } else {
                                callback.onError();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onError();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        if (callback != null) {
                            callback.onError();
                        }
                    }
                });
    }

    interface BatchbuyResult {
        void onSuccess(EntityBean[] beans);

        void onError();
    }


    /**
     * 免费试看的数据 最多9个
     */
    public static void getFreeCourse(final BatchbuyResult callback) {
        //第一次加载的时候先使用本地数据
        EntityBean[] course_sfreeCourses = CacheUtils.getCacheFirstData("course_sfreeCourses");
        if (callback != null) {
            callback.onSuccess(course_sfreeCourses);
        }


        EntityBean bean = new EntityBean();
        bean.set("pagesize", "9");
        bean.set("pagenum", "1");
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl,
                "course_sfreeCourses", fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int value = (Integer) bean.get("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (value == 1) {
                                EntityBean resultBean = bean.getBean("result");
                                EntityBean[] results = resultBean.getBeans("result");
                                CacheUtils.cacheFirstData("course_sfreeCourses", results);
                                if (callback != null) {
                                    callback.onSuccess(results);
                                }
                            } else {
                                callback.onError();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onError();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        if (callback != null) {
                            callback.onError();
                        }
                    }
                });
    }

    /**
     * 获取是否有hra的数据
     */
    public static void getHraData(final HraResult hracallback) {
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_getVipInfo",
                fbean, new LoadDataManager.OnRequestCallListener2() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            Integer resultstate = bean.getInt("resultstate");
                            if (resultstate == 1) {
                                EntityBean resultBean = bean.getBean("result");
                                int harisfree = resultBean.getInt("harisfree");
                                if (harisfree == 0) {
                                    hracallback.onSuccess(false);
                                } else {
                                    hracallback.onSuccess(true);
                                }
                            } else {
                                hracallback.onError();
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            hracallback.onError();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        hracallback.onError();
                    }

                });
    }

    interface HraResult {
        void onSuccess(boolean isShow);

        void onError();
    }
}
